/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.controller;

/**
 *
 * @author joep
 */
import com.bayu.services.dao.AvgStopTimeDao;
import com.bayu.services.dao.Graph2mlDao;
import com.bayu.services.entity.AvgStopTime;
import com.bayu.services.entity.Graph2ml;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/avg-pms-input.htm")
public class AvgPmsInput {
    private AvgStopTimeDao dao;

    @Autowired
    public void setDao(AvgStopTimeDao dao) {
        this.dao = dao;

    }

    @RequestMapping(method=RequestMethod.GET)
    public void displayForm(ModelMap map, String id) {
    }

    @RequestMapping(method = RequestMethod.POST)
    public String proccessForm(@ModelAttribute("AvgStopTime") AvgStopTime aStop, BindingResult result,ModelMap map,HttpServletRequest req){

       
        this.dao.save(aStop);
                 return "redirect:view-avg-pms-data.htm?msg=Data has been updated";

        //DateFormat formarter = new SimpleDateFormat("yy-MM-dd");
            //Date tanggal;


          /*  if(result.hasFieldErrors("tahun") && result.hasFieldErrors("jenis")){
                try {
                    //tanggal = formarter.parse(date);
                    g2ml.setDate(tanggal);

                } catch (ParseException ex) {
                    Logger.getLogger(CompInput.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            //List total = this.dao.custom("select count(*) from Graph2ml where date='" + date + "'");

            //String[] tempDate = date.split("-");

            String tahun = (String) result.getFieldValue("tahun");
            String jenis = (String) result.getFieldValue("jenis");

            List total = this.dao.custom("select count(*) from AvgStopTime where tahun='" + tahun + "' AND jenis='"+jenis+"'");
            String strTotal=total.get(0).toString();


            int c = Integer.valueOf(strTotal).intValue();
           // System.out.println(strTotal+"---"+c+"--"+date);
            if(c > 0){
                return "redirect:ErrorDuplicate.html";
            }else{
                 this.dao.save(aStop);
                 return "redirect:view-avg-pms-data.htm?msg=Data has been updated";
            }*/
    }

}
